package eoeProject.edenOfEast.page.analyses.analysesTop.component;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.LinkedList;
import java.util.List;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.request.mapper.parameter.PageParameters;

import eoeProject.edenOfEast.bean.EOEAnalysesBean;
import eoeProject.edenOfEast.core.DBConnector;
import eoeProject.edenOfEast.core.SQLMaker;
import eoeProject.edenOfEast.dataBase.DB_EOE_ANALYSES;
import eoeProject.edenOfEast.utils.AnalysesUtils;

//@AuthorizeInstantiation("ADMIN")
public class AnalysesTopListPanel extends Panel {
	private static final long serialVersionUID = 1L;

	public final static Integer TITLE_MAX_LENGTH = 10;

	public final static Integer ABOUT_MAX_LENGTH = 23;

	List<EOEAnalysesBean> beanList = new LinkedList<EOEAnalysesBean>();

	public AnalysesTopListPanel(String id) {
		super(id);
		SQLMaker sql = new SQLMaker(DB_EOE_ANALYSES.TABLE_NAME,
				SQLMaker.SQL_TYPE_SELECT);
		sql.addExpEquals(DB_EOE_ANALYSES.STATUS, 1);
		sql.addOrderBy(DB_EOE_ANALYSES.UPDATE_DATE, "desc");
		sql.addLimit(10);
		ResultSet rs = DBConnector.getInstance().get(sql.getSQL());
		try {
			while (rs.next()) {
				EOEAnalysesBean bean = new EOEAnalysesBean();
				bean.setAnalysesId(rs.getInt(DB_EOE_ANALYSES.ANALYSES_ID));
				bean.setTitle(rs.getString(DB_EOE_ANALYSES.TITLE));
				bean.setCategoryId(rs.getInt(DB_EOE_ANALYSES.CATEGORY_ID));
				bean.setUpdateDate(rs.getTimestamp(DB_EOE_ANALYSES.UPDATE_DATE));
				bean.setAbout(rs.getString(DB_EOE_ANALYSES.ABOUT));

				beanList.add(bean);
			}
			rs.close();
		} catch (SQLException e) {
			System.out.println("AnalysesTopListPanelException: "
					+ e.getMessage());
		}

		ListView<Object> latestAnalyseslist = new ListView<Object>(
				"latestAnalyseslist", beanList) {
			private static final long serialVersionUID = -6962499153966966392L;

			@SuppressWarnings("rawtypes")
			@Override
			protected void populateItem(ListItem item) {
				final EOEAnalysesBean bean = (EOEAnalysesBean) item
						.getModelObject();

				PageParameters parameters = new PageParameters();
				parameters.add("analysesId", bean.getAnalysesId());
				BookmarkablePageLink<WebPage> link = new BookmarkablePageLink<WebPage>(
						"link",
						eoeProject.edenOfEast.page.analyses.analysesRes.Index.class,
						parameters);
				String titleValue = bean.getTitle();
				if (titleValue.length() > TITLE_MAX_LENGTH) {
					titleValue = bean.getTitle().substring(0,
							TITLE_MAX_LENGTH - 1);
					titleValue = titleValue + "…";
				}
				link.add(new Label("title", titleValue));
				item.add(link);
				item.add(new Label("category", AnalysesUtils
						.getCategoryName(bean.getCategoryId())));
				SimpleDateFormat sdf = new SimpleDateFormat("M月d日 H時m分");
				item.add(new Label("updateDate", sdf.format(bean
						.getUpdateDate())));
				String aboutValue = bean.getAbout();
				if (aboutValue.length() > ABOUT_MAX_LENGTH) {
					aboutValue = bean.getAbout().substring(0,
							ABOUT_MAX_LENGTH - 1);
					aboutValue = aboutValue + "…";
				}
				item.add(new Label("about", aboutValue));

				// Link reciteLink = new Link("reciteLink") {
				// @Override
				// public void onClick() {
				// setResponsePage(new RecitePage(vo));
				// }
				// };
				//
				// item.add(reciteLink);

				// reciteLink.add(new Label(MagicVO.NAME_COL));
			}

		};

		add(latestAnalyseslist);

	}

}
